__author__ = 'Knight'
import datetime

def now():
    return datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')

def today():
    return datetime.datetime.now().strftime('%Y-%m-%d')

def yesterday():
    return (datetime.datetime.now() - datetime.timedelta(days=1)).strftime('%Y-%m-%d')

def today_8():
    return today().replace('-', '')

def yesterday_8():
    return yesterday().replace('-', '')

def n_days_before(idx=0, date_str=None, date_format='%Y-%m-%d'):
    date_start = datetime.datetime.now()
    if date_str:
        date_start = datetime.datetime.strptime(date_str, date_format)
    return (date_start - datetime.timedelta(days=idx)).strftime(date_format)

def n_days_before_8(idx=0):
    return n_days_before(idx).replace('-', '')

def get_today_lm(date_init, date_format='%Y-%m-%d'):
    date_date = datetime.datetime.strptime(date_init, date_format)
    date_first_day = datetime.date(day=1, month=date_date.month, year=date_date.year)
    lm_ld = date_first_day - datetime.timedelta(days=1)
    out_day = date_date.day
    if lm_ld.day < out_day:
        out_day = lm_ld.day
    res = datetime.date(day=out_day, month=lm_ld.month, year=lm_ld.year).strftime(date_format)
    return res

def util_date_add(date_init, interval, date_format='%Y-%m-%d'):
    """
    :param date_init: 日期字符串
    :param interval: 天增加量
    :param date_format: 字符串格式
    :return: 增加后的日期字符串
    """
    date_init = datetime.datetime.strptime(date_init, date_format)
    date_init = date_init + datetime.timedelta(interval)
    date_init = date_init.strftime(date_format)
    return date_init

def get_date_by_interval(date_start, date_end, interval=None, date_format=None):
    interval = 1 if interval is None else interval
    date_init = date_start
    while date_init <= date_end:
        yield date_init
        date_init = util_date_add(date_init, interval) if date_format is None \
            else util_date_add(date_init, interval, date_format)

def get_month_by_interval(date_start, date_end):
    """
    :param date_start: 2019-01
    :param date_end: 2019-04
    :return: 2019-01, 2019-02, 2019-03, 2019-04
    """

    assert date_start <= date_end
    assert len(date_start) == 7 and len(date_end) == 7

    rst = []
    date_now = date_start

    while date_now <= date_end:
        rst.append(date_now)
        _year = int(date_now[:4])
        _month = int(date_now[-2:])
        if _month == 12:
            _month = 1
            _year += 1
        else:
            _month += 1

        _month_str = '0{}'.format(_month)[-2:]
        date_now = '{}-{}'.format(_year, _month_str)
    return rst

def interval_sce(t_max, t_min, _format='%Y-%m-%d %H:%M:%S', rt=None):
    if isinstance(t_max, str):
        t_max = datetime.datetime.strptime(t_max, _format)
    if isinstance(t_min, str):
        t_min = datetime.datetime.strptime(t_min, _format)

    if not rt:
        _i = (t_max - t_min).total_seconds()
        return int(_i)
    if rt == 'days':
        _i = (t_max - t_min).total_seconds() / 86400

        return int(_i)

def get_month(day_par):
    t_day = datetime.datetime.strptime(day_par, "%Y%m%d")
    last_month = t_day + datetime.timedelta(days=-t_day.day)
    month = last_month.strftime("%Y%m")
    return month


if __name__ == '__main__':
    print(interval_sce('20200818', '20200316', _format='%Y%m%d', rt='days'))
